package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Dailog.PayDialog;
import adapter.BuyClassTimeAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.BuyClassTimeBean;
import bean.GoodsPay;
import event.OrderEvent;
import student.andlei.com.student.R;
import ui.fragment.MineFragment;
import utils.GsonUtils;

/**
 * 购买课时
 */
public class BuyClassTimeFragment extends BaseNetFragment {
    private ImageView img_back;
    private RecyclerView recyclerView;
    private BuyClassTimeAdapter adapter;
    private List<BuyClassTimeBean> list = new ArrayList<>();
    private TextView tv_explain,
                    tv_money;
    private CheckBox ck_weixin,
                    ck_zhifubao;
    private Button bt_buy;
    private int study_goods_id;
    private int  pay_type;
    private PayDialog mPayDialog;
    public static Fragment getInstance() {
        return new BuyClassTimeFragment();
    }
    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            if(action.equals("personal/getGoodsList")){
                list = GsonUtils.getInstance(context).parseHaveHeaderJArray(baseBean,BuyClassTimeBean.class,"list");
                adapter.setNewData(list);
            }else if(action.equals("Shop/payment")){
                final GoodsPay goodsPay = (GoodsPay) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data), GoodsPay.class);
                if (!TextUtils.isEmpty(goodsPay.qr_code_img)) {
                    if (mPayDialog != null && mPayDialog.isShowing()) {
                        mPayDialog.dismiss();
                    }
                    mPayDialog = new PayDialog(getActivity(), goodsPay.qr_code_img, goodsPay.order_id);
                    mPayDialog.setOnPayListener(new PayDialog.OnPayListener() {
                        @Override
                        public void onPay(boolean isSucceed) {
                            if (isSucceed) {
                                toast("支付成功");
                                mPayDialog.dismiss();
                            }else {
                                toast("支付失败");
                                mPayDialog.dismiss();
                            }
                        }
                    });
                    mPayDialog.show();
                }
            }
        }else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_buyclasstimel;
    }

    @Override
    protected void findViews() {
        img_back = getView(R.id.img_back);
        recyclerView = getView(R.id.rcly_);
        tv_explain = getView(R.id.tv_buy_explain);
        tv_money = getView(R.id.tv_money);
        ck_weixin = getView(R.id.check_weixin);
        ck_zhifubao = getView(R.id.check_zhifubao);
        bt_buy = getView(R.id.bt_zhifu);
    }

    @Override
    protected void formatViews() {
        img_back.setOnClickListener(this);
        bt_buy.setOnClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        adapter = new BuyClassTimeAdapter(getActivity(),R.layout.item_buyclasstime,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
               for(int i = 0;i<list.size();i++){
                if(i == position){
                    list.get(i).setIsChecked(1);
                    tv_money.setText("支付金额：￥"+list.get(i).price);
                    study_goods_id = list.get(i).study_goods_id;
                }else {
                    list.get(i).setIsChecked(0);
                }
               }
               adapter.notifyDataSetChanged();
            }
        });
        //微信选择框
        ck_weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    pay_type = 1;
                    ck_zhifubao.setChecked(false);
                }else {
                    pay_type = 2;
                    ck_zhifubao.setChecked(true);
                }
            }
        });
        //支付宝选择框
        ck_zhifubao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    pay_type = 2;
                    ck_weixin.setChecked(false);
                }else {
                    pay_type = 1;
                    ck_weixin.setChecked(true);
                }
            }
        });
    }

    @Override
    protected void formatData() {
        tv_explain.setText("1课时购买后，可直接选择课程进行上课学习\n" +
                "2.支付及上课中遇到问题请联系平台客服，客服电话：400-000-0001");
        loadData();
//        for (int i=0;i<6;i++){
//            BuyClassTimeBean bean = new BuyClassTimeBean();
//            bean.setIsChecked(0);
//            bean.setClassTimeNumber((i+1)+"课时");
//            bean.setMoney(i+"8元");
//            bean.setOldMoney("原价：1000元");
//            list.add(bean);
//        }
    }

    @Override
    protected void getBundle() {

    }
    private void loadData(){
        post("personal/getGoodsList",BaseBean.class,false);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_back:
                ((MineFragment)(this.getParentFragment())).GojumpMyClassTimeFragment();
                break;
            //购买课时
            case R.id.bt_zhifu:
                Map<String,Object> map =  new HashMap<>();
                map.put("order_id",""+study_goods_id);
                map.put("order_type","1");
                map.put("pay_type",""+pay_type);
                post("Shop/payment",map,BaseBean.class,false);
                break;

        }
    }

}
